package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSBusOrgProcessor;
import com.hitrust.gs.business.GSBusOrderProcesser;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.form.GSOrderAuthorizeQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.*;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSOrderAuthorizeQueryVO;

public class GSOrderAuthorizeQueryAction extends GSBaseAction {
    public String execute(GSBaseForm aForm) throws GSException {
        LoginContext operatorBean = (LoginContext) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        GSBusOrderProcesser ordProcesser = new GSBusOrderProcesser();
        GSOrderAuthorizeQueryForm tForm = (GSOrderAuthorizeQueryForm) aForm;
        GSOrderAuthorizeQueryVO oaqVO = new GSOrderAuthorizeQueryVO();
        oaqVO.orderDetailId = tForm.getOrderDetailId();
        oaqVO.deptId = tForm.getOrderDept();
        oaqVO.addr = tForm.getDeliverGoodsAddrId();
        oaqVO.concentricityId = tForm.getConcentricityId();
        oaqVO.manufInfo = tForm.getManufInfo();
        oaqVO.beginDate = tForm.getOrderCreateDateStr();
        oaqVO.endDate = tForm.getOrderCreateDateEnd();
        oaqVO.providerName = tForm.getProviderName();
        oaqVO.manuftype1 = tForm.getManuftype1();
        oaqVO.manuftype2 = tForm.getManuftype2();
        oaqVO.manuftype3 = tForm.getManuftype3();
        oaqVO.drawingpiececode = tForm.drawingpiececode;
        if (aForm.getRequest().getAttribute("ISQUERYCONDITIONEXIST") != null) {
            // 是执行完返回的，读出查询条件
            oaqVO = (GSOrderAuthorizeQueryVO) aForm.getSession().getAttribute("QUERYCONDITION");
        } else {
            // 初次查询，缓存查询条件
            aForm.getSession().setAttribute("QUERYCONDITION", oaqVO);
        }
        try {
            String[] tStr = this.getPageParams(aForm, oaqVO);
            // 先查询出当前初审人员有权审核的所有订单
            ArrayList orderDetailAry = ordProcesser.getAllAuthorizeOrderDetail(oaqVO, Integer.parseInt(tStr[0]), Integer.parseInt(tStr[1]));
            ResultProcess tRsProc = new ResultProcess();
            tRsProc.setResult(orderDetailAry);
            tForm.getRequest().setAttribute("orderlist", tRsProc);
            // 存储分页参数
            tForm.getRequest().setAttribute("pageSize", tStr[1]);
            tForm.getRequest().setAttribute("totalRecords", tStr[2]);
            // 查询产品等级
            GSManufactureProcessor mproc = new GSManufactureProcessor();
            mproc.getManufactureOfLevel(aForm);
            GSUniteGwProcessor processer = new GSUniteGwProcessor();
            ArrayList ary = processer.getWareHouseLevelName(operatorBean.getLoginWarehouseCode(),operatorBean.getLoginWarehouseCode());
            aForm.getRequest().setAttribute("organdaddr",ary);
        } catch (GSException e) {
            aForm.getRequest().setAttribute("action", "A150201.do");
            throw e;
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            tForm.getRequest().setAttribute("action", "A150201.do");
            throw new GSException(GSException.E10030, GSException.E10030_MSG);
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

    /**
     * 取得分页参数
     * 
     * @param aForm
     *            基类form
     * @param aOrderVO
     *            订单VO
     * @param aOrg
     *            组织ArrayList对象
     * @return
     * @throws Exception
     */
    private String[] getPageParams(GSBaseForm aForm, GSOrderAuthorizeQueryVO tGSOrderAuthorizeQueryVO) throws Exception {
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        GSBusOrderProcesser ordProcesser = new GSBusOrderProcesser();
        // 根据页面传入的查询参数查询订单信息
        if (aForm.getChoice() == null || aForm.getChoice().equals("")) {
            totalRecords = ordProcesser.getAllAuthorizeOrderDetailCount(tGSOrderAuthorizeQueryVO);

            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0) ? totalRecords / pageSize : totalRecords / pageSize + 1;
            if (pageNo > n) {
                pageNo = n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        } else {
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }
}
